import matplotlib.pyplot as plt
import numpy as np
import json
from scipy.stats import gaussian_kde

# Load data
with open("plot/Q-value-distribution/phi3-sampling/success_q_list.json") as f:
    success_data1 = json.load(f)

with open("plot/Q-value-distribution/phi3-with-q-sampling/success_q_list.json") as f:
    success_data2 = json.load(f)

with open("plot/Q-value-distribution/phi3-sampling/failure_q_value_list.json") as f:
    failure_data = json.load(f)

# Combine success data
success_data = success_data1 + success_data2

# Create a range of values for x-axis
x_values = np.linspace(min(success_data + failure_data), max(success_data + failure_data), 1000)

# Perform Kernel Density Estimation (KDE) for each dataset
success_kde = gaussian_kde(success_data[:200])
failure_kde = gaussian_kde(failure_data[:200])

# Set up the plot
plt.figure(figsize=(8, 6))

# Plot the KDEs with shading
plt.plot(x_values, success_kde(x_values), label='Action to Success', color='#1f77b4', linewidth=2)
plt.fill_between(x_values, 0, success_kde(x_values), color='#1f77b4', alpha=0.3)

plt.plot(x_values, failure_kde(x_values), label='Action to Failure', color='#ff7f0e', linewidth=2)
plt.fill_between(x_values, 0, failure_kde(x_values), color='#ff7f0e', alpha=0.3)

plt.xlabel('Q Value', fontsize=22)
plt.ylabel('Density', fontsize=22)

# Add gridlines
plt.grid(True, linestyle='--', alpha=0.7)

# Set the font size of ticks
plt.xticks(fontsize=22)
plt.yticks(fontsize=22)

# Remove top and right spines
ax = plt.gca()
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)

# Show legend with a larger font size
plt.legend(fontsize=17)

# Save the plot to a file with a tight layout
plt.savefig('plot/Q-value-hist.png', dpi=300, bbox_inches='tight')


